@echo off
REM # Open-ATC-lab release build script 1.0
REM # --------------------------------------

REM #
REM # Try to adopt vs2010 env variable set
REM #
echo Checking Visual Studio 2010 environment variables...
IF DEFINED VS100COMNTOOLS call "%VS100COMNTOOLS%\vsvars32.bat"
IF %ERRORLEVEL% NEQ 0 goto :enverrorvs
IF NOT DEFINED VS100COMNTOOLS goto :enverrorvs

REM #
REM # Check project specific env variables
REM #
echo Checking project specific environment variables...
IF NOT DEFINED ATCLAB goto :enverror
echo Env: ATCLAB=%ATCLAB%
IF NOT DEFINED QTDIR goto :enverror
echo Env: QTDIR=%QTDIR%
IF NOT DEFINED XERCESCROOT goto :enverror
echo Env: XERCESCROOT=%XERCESCROOT%

REM #
REM # Get release name...
REM #
SET RELEASENAME=%1
IF [%1] EQU [] SET /P RELEASENAME=Please enter build number (e.g. 0.0.1.1): 
echo Version: %RELEASENAME% > VERSION

REM #
REM # Make release
REM #
echo Building release...
%QTDIR%\bin\qmake ..\atclab.google\atclab.pro && nmake release-clean && nmake release 
IF %ERRORLEVEL% NEQ 0 goto :buildfailed

REM #
REM # Create zip vbs routine
REM #
echo Set objArgs = WScript.Arguments > _zip.vbs
echo ZipFile = objArgs(0) >> _zip.vbs
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zip.vbs
echo With CreateObject("Shell.Application") >> _zip.vbs
echo for i = 1 to objArgs.Count - 1  >> _zip.vbs
echo WScript.Echo "Copying " ^& objArgs(i)  >> _zip.vbs
echo .NameSpace(ZipFile).CopyHere objArgs(i) >> _zip.vbs
echo next  >> _zip.vbs
echo End with >> _zip.vbs
echo WScript.Sleep 3000 >> _zip.vbs

REM #
REM # Create release zip
REM #
echo Building release zip... %RELEASENAME%.zip

CScript _zip.vbs %cd%\%RELEASENAME%.zip %cd%\Release\atclab.exe %XERCESCROOT%\bin\xerces-c_3_1.dll %QTDIR%\bin\Qt3Support4.dll %QTDIR%\bin\QtCore4.dll %QTDIR%\bin\QtGui4.dll %QTDIR%\bin\QtNetwork4.dll %QTDIR%\bin\QtSql4.dll %QTDIR%\bin\QtXml4.dll %cd%\COPYING %cd%\VERSION
IF %ERRORLEVEL% NEQ 0 goto :zipfailed

echo Done building release package.
goto :done

:enverrorvs
echo Environment variable VS100COMNTOOLS was not defined.
echo Visual Studio 2010 is required for this build script.
goto :done

:enverror
echo One or more of the required environment variables was not defined.
echo Please ensure the following are correctly defined: ATCLAB, QTDIR, XERCESCROOT
echo For details please see the build environment instructions:
echo http://code.google.com/p/atclab/wiki/HowToVC2010ExpressBuildEnvironment
goto :done

:buildfailed
echo Release build failed; Cannot continue.
goto :done


:zipfailed
echo Writing to %RELEASENAME%.zip has failed...
goto :done

:done
REM #
REM # Cleanup
REM #
IF EXIST _zip.vbs DEL /q _zip.vbs
IF EXIST VERSION DEL /q VERSION

pause